package cn.sanxintong.utils.freemarker;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;
import cn.sanxintong.entity.constant.Constant;
import cn.sanxintong.entity.constant.PackConstant;

/**
 * freemarker 模板工具类
 * @author 31439
 *
 */
public class FreeMarkerTemplateUtils {
	
	private FreeMarkerTemplateUtils(){
		
	}
	
    private static final Configuration CONFIGURATION = new Configuration();

    static{
    	try {
    		//这里比较重要，用来指定加载模板所在的路径
			CONFIGURATION.setDirectoryForTemplateLoading(new File("E:\\workspace\\sanxintong-generatecode\\WebContent\\templates"));
			//CONFIGURATION.setTemplateLoader(new ClassTemplateLoader(FreeMarkerTemplateUtils.class, "D:\\test1\\"));
	        CONFIGURATION.setDefaultEncoding("UTF-8");
	        CONFIGURATION.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
	        CONFIGURATION.setCacheStorage(new freemarker.cache.MruCacheStorage(20, 250));
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

    /**
     * 获得模板
     * @param templateName
     * @return
     * @throws IOException
     */
    public static Template getTemplate(String templateName) throws IOException {
        try {
            return CONFIGURATION.getTemplate(templateName);
        } catch (IOException e) {
            throw e;
        }
    }

    /**
     * 清除缓存
     */
    public static void clearCache() {
        CONFIGURATION.clearTemplateCache();
    }
    
    /**
     * 通过模板生成可以使用的文件
     * @param templateName
     * @param file
     * @param dataMap
     * @throws Exception
     */
    public static void generateFileByTemplate(final String templateName,File file,Map<String,Object> dataMap) throws Exception{
        Template template = getTemplate(templateName);
        FileOutputStream fos = new FileOutputStream(file);
        dataMap.put("table_name_small",Constant.TABLEN_NAME);
        dataMap.put("table_name",StringUtils.substring(Constant.CHANGE_TABLE_NAME, 3));
        dataMap.put("author",Constant.AUTHOR);
        dataMap.put("date",Constant.CURRENT_DATE);
        dataMap.put("package_name",Constant.PACKAGE_NAME);
        
        //各文件所在的包
        dataMap.put("entity_package_name",PackConstant.ENTITY_PACKAGE);
        dataMap.put("dto_package_name",PackConstant.ENTITY_PACKAGE);
        dataMap.put("controller_package_name",PackConstant.CONTROLLER_PACKAGE);
        dataMap.put("service_package_name",PackConstant.SERVICE_PACKAGE);
        dataMap.put("dao_package_name",PackConstant.DAO_PACKAGE);
        dataMap.put("serviceImpl_package_name",PackConstant.SERVICE_IMPL_PACKAGE);
        dataMap.put("mapper_package_name",PackConstant.MAPPER_PACKAGE);
        
        dataMap.put("table_annotation",Constant.TABLE_ANNOTATION);
        Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"),10240);
        template.process(dataMap,out);
    }
}
